package com.cjb.cloudsecurity.userMsg.service.impl;

import com.cjb.cloudsecurity.userMsg.entity.UserDetail;
import lombok.AllArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
 * All rights Reserved, Designed By  chenjinbin
 *
 * @author chenjinbin
 * @Project: fjbd_cjb_bs
 * @Package com.cjb.cloudsecurity.userMsg.service.impl
 * @title SecurityUserServiceImpl
 * @Description
 * @email
 * @date 2023-08-30 14:47
 * @since JDK 1.8
 */
@Service
@AllArgsConstructor
public class SecurityUserServiceImpl {
    /**
     * 获取用户信息
     */
    public  UserDetail getUser() {
        UserDetail user;
        try {
            user = (UserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {
            return null;
        }

        return user;
    }

    /**
     * 获取用户ID
     */
    public  Long getUserId() {
        UserDetail user = getUser();
        if (user == null) {
            return null;
        }
        return user.getId();
    }
}
